/*
Description:    FIUMAPS.Cache is an array of objects that caches the session's results
                on the client's browser.
Author:         Carlos Krefft (krefftc@gmail.com)

Example Data:   FIUMAPS.Cache.Data = {"MMC":{Locations:[],Contents:[]},{"BBC":{Locations:[],Contents:[]}}

                FIUMAPS.Cache.Data["MMC"].Locations = LocationCacheItem[]
                FIUMAPS.Cache.Data["MMC"].Contents = {"Categories":[1,5,7] , "Letters":["A","D","R"], "IDs":{Services:[],Buildings:[]}}}
                Contents keeps track of all the category ids and letters that have been loaded into cache.
*/

//Contents: {Categories:[], Letters: []}
FIUMAPS.Cache = { Data:null,
    isLetterLoaded: function (letter) {
        //check to see if the letter is in Data Cache by checking if found in Contents.
        return (this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.Letters.indexOf(letter) > -1) ? true : false
    }, 
    isCategoryLoaded: function (category_id) {
        //check to see if the letter is in Data Cache by checking if found in Contents.
        return (this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.Categories.indexOf(category_id) > -1) ? true : false
    },     
    setCategoryLoaded: function (category_id) {
        this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.Categories.push(category_id);
    },     
    setLetterLoaded: function (letter) {
        this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.Letters.push(letter);
    },       
    AddLocationCacheItem: function (locationCacheItem) {
        //add item to cache.
        this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Locations.push(locationCacheItem);
        if(locationCacheItem.Attributes.SERVICE_ID){
            //check to see if item is already cached.
            if(!this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.IDs.Services[locationCacheItem.Attributes.SERVICE_ID]){                    
                this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.IDs.Services.push(locationCacheItem.Attributes.SERVICE_ID);
            }
               
        }else{
            //check to see if item is already cached.
            if(!this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.IDs.Services[locationCacheItem.Attributes.BUILDING_ID])
                this.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Contents.IDs.Buildings.push(locationCacheItem.Attributes.BUILDING_ID);
        }   

    },
    InitiateCache: function() {
        //load campus cache structure from CampusInfo Data.
        //Will loop number of campus times.
        var tmpArr = [];
        for (var z = 0; z < FIUMAPS.CampusInfo.arrData.length; z++) {
            tmpArr.push(FIUMAPS.CampusInfo.arrData[z].Code + ":{Locations:[],Contents:{Categories:[], Letters:[], IDs:{Services:[],Buildings:[]}}}");
        }      
        var JSONString = "FIUMAPS.Cache.Data = {" + tmpArr.join(',') + "}"; 
        eval(JSONString); // Execute the javascript code contained in JSONString.
        
    },     
    LocationCacheItem: function(type, letter, attributes, geometries) {
        this.Type = type;
        this.Letter = letter;
        this.Attributes = attributes;
        this.Geometries = geometries;          
    }  
    
};
FIUMAPS.Cache.InitiateCache();